<template>
  <div>
    <el-card style="margin: 20px">
      <el-form style="width: 100%" size="small" :inline="true" >
        <el-form-item>
          <el-input placeholder="用户名" v-model="roleName"></el-input>
        </el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="searchRole" >查询</el-button>
        <el-button type="default" size="small" style="padding: 10px" @click="resetRole">清空</el-button>
      </el-form>
      <el-button size="small" type="primary" @click="addRole">添加</el-button>
      <el-button size="small" type="danger" disabled>批量删除</el-button>

      <el-table
        ref="multipleTable"
        border
        :data="RoleList"
        tooltip-effect="dark"
        style="margin-top: 20px"
      >
        <el-table-column
          type="selection"
          width="100">
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称"
          width="600px"
          show-overflow-tooltip>
         <template v-slot="{row}">
           <template v-if="row.isShow">
              <el-input style="width: 400px" size="small" v-model="row.roleName"></el-input>
              <el-button size="small" style="margin-left: 10px"  type="warning" icon="el-icon-refresh" @click="cancelRole(row)">取消</el-button>
           </template>
           <span v-else>{{row.roleName}}</span>
         </template>
        </el-table-column>


        <el-table-column
          prop="name"
          label="操作"
        >
          <template v-slot="{row}">
            <el-button type="info" size="small" icon="el-icon-info"></el-button>
            <el-button type="primary" size="small" icon="el-icon-check" @click="updateRoleInfo(row)"></el-button>
            <el-button type="primary" size="small" icon="el-icon-edit-outline" @click="updateRole(row)"></el-button>

            <el-popconfirm
              title="这是一段内容确定删除吗？"
            >
              <template #reference>
                <el-button type="danger" style=" margin: 10px 10px" size="small"  icon="el-icon-delete-solid"></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px"
        :current-page="currentPage"
        :page-sizes="[3, 5, 7, 9]"
        :page-size="pageSize"
        layout="prev, pager, next,jumper, sizes,total"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog
      title="添加角色"
      :visible.sync="dialogVisible"
      width="25%"
      >
      <el-form :model="RoleInfo">
        <el-form-item label="请输入新的角色名称">
          <el-input v-model="RoleInfo.roleName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="addRoleData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {reqRolePageLimit, reqRoleSave, reqRoleUpdate} from "@/api/role";

export default {
  name: "Role",
  data(){
    return{
      //当前页
      currentPage:1,
      //每页条数
      pageSize:5,
      //总页数
      total:0,
      //角色管理信息
      RoleList:[],
      //控制添加面板的数据
      dialogVisible:false,
      //收集添加的数据
      RoleInfo:{
        roleName: ""
      },
      //搜索的数据
      roleName:""
    }
  },
  methods:{
    //角色管理分页请求
    async getRolePageLimit(){
      try {
       const result =   await reqRolePageLimit(this.currentPage,this.pageSize)
       this.total = result.total
       this.RoleList = result.items.map(item=>{
         item.isShow = false,
         item.keepRoleName = item.roleName // 缓存角色名称
         return item
       })
      }catch (e){
        this.$message.error(e.message)
      }
    },
    //控制每页条数改变
    handleSizeChange(val) {
      this.pageSize = val
      this.getRolePageLimit()
    },
    //控制当前页改变
    handleCurrentChange(val) {
      this.currentPage = val
      this.getRolePageLimit()
    },
    //添加角色
    addRole(){
      this.dialogVisible = true
    },
    //取消添加
    cancel(){
      this.dialogVisible = false
      this.$message.warning('取消添加');
    },
    //添加角色用户
    async addRoleData(){
       try {
        await reqRoleSave(this.RoleInfo)
         this.$message.success('添加成功');
       }catch (e){
         this.$message.error(e.message);
       }
       this.dialogVisible = false
       this.getRolePageLimit()
    },
    //点击修改角色用户状态
    updateRole(row){
      row.isShow = true
    },
    //确认时改变数据
    async updateRoleInfo(row){
      row.isShow = false
      try {
        await reqRoleUpdate(row)
        this.$message.success('更新数据成功');
      }catch (e){
        this.$message.error(e.message);
      }
      this.getRolePageLimit()
    },
    //取消角色更改
    cancelRole(row){
      row.roleName = row.keepRoleName
      row.isShow = false
      this.$message.warning('取消角色修改')
    },

    //搜索逻辑
    async searchRole(){
      try {
        const result =  await reqRolePageLimit(this.currentPage,this.pageSize,this.roleName)
        this.RoleList = result.items
      }catch (e){
        this.$message.error(e.message)
      }
    },
    //清空搜索输入框
    resetRole(){
      this.RoleList = [];
      this.roleName = "";
      this.getRolePageLimit()
    },
    //删除单个角色
    async deleteRole(id){
      try {

      }catch (e){
        this.$message.error(e.message)
      }
    }
  },
  mounted() {
    this.getRolePageLimit()
  }
}
</script>

<style>

</style>
